package com.egao.common.module.integral.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.module.integral.entity.IntegralAggregate;
import com.egao.common.module.integral.mapper.IntegralAggregateMapper;
import com.egao.common.module.integral.service.IntegralAggregateService;
import org.springframework.stereotype.Service;

/**
 * 积分总数接口实现类
 * @author pyx
 * @date 2021/5/10
 */
@Service
public class IntegralAggregateServiceImpl extends ServiceImpl<IntegralAggregateMapper, IntegralAggregate> implements IntegralAggregateService {
    @Override
    public IntegralAggregate studentIntegral(Integer studentId) {
        if(studentId == null){
            throw new ParameterException("学生信息不存在");
        }

        IntegralAggregate integralAggregate = baseMapper.selectById(studentId);
        if(integralAggregate == null){
            IntegralAggregate studentIntegral = new IntegralAggregate();
            studentIntegral.setStudentId(studentId);
            studentIntegral.setAllIntegral(0);
            baseMapper.insert(studentIntegral);
            return studentIntegral;
        }

        return integralAggregate;
    }

    @Override
    public void updateIntegral(Integer studentId,Integer integer) {
        if(integer == null){
            throw new ParameterException("积分数不能为空");
        }

        IntegralAggregate integralAggregate = studentIntegral(studentId);
        Integer integralAll = integralAggregate.getAllIntegral();
        integralAll = integralAll + integer;
        integralAggregate.setAllIntegral(integralAll);
        baseMapper.updateById(integralAggregate);
    }
}
